package karma.main;

import java.util.ArrayList;
import java.util.Iterator;
import net.minecraft.client.Minecraft;
import net.minecraft.src.*;

public class ItemHolyPickaxe extends ItemTool
{
    int bi;
    int md;
    private static Block blocksEffectiveAgainst[];

    public ItemHolyPickaxe(int i, EnumToolMaterial enumtoolmaterial)
    {
        super(i, 2, enumtoolmaterial, blocksEffectiveAgainst);
    }

    /**
     * Returns if the item (tool) can harvest results from the block type.
     */
    public boolean canHarvestBlock(Block block)
    {
        if (block == Block.obsidian)
        {
            return toolMaterial.getHarvestLevel() == 3;
        }

        if (block == Block.blockDiamond || block == Block.oreDiamond)
        {
            return toolMaterial.getHarvestLevel() >= 2;
        }

        if (block == Block.blockGold || block == Block.oreGold)
        {
            return toolMaterial.getHarvestLevel() >= 2;
        }

        if (block == Block.blockSteel || block == Block.oreIron)
        {
            return toolMaterial.getHarvestLevel() >= 1;
        }

        if (block == Block.blockLapis || block == Block.oreLapis)
        {
            return toolMaterial.getHarvestLevel() >= 1;
        }

        if (block == Block.oreRedstone || block == Block.oreRedstoneGlowing)
        {
            return toolMaterial.getHarvestLevel() >= 2;
        }

        if (block.blockMaterial == Material.rock)
        {
            return true;
        }
        else
        {
            return block.blockMaterial == Material.iron;
        }
    }

    /**
     * Returns the strength of the stack against a given block. 1.0F base, (Quality+1)*2 if correct blocktype, 1.5F if
     * sword
     */
    public float getStrVsBlock(ItemStack itemstack, Block block)
    {
        if (block != null && (block.blockMaterial == Material.iron || block.blockMaterial == Material.rock))
        {
            return efficiencyOnProperMaterial;
        }
        else
        {
            return super.getStrVsBlock(itemstack, block);
        }
    }

    public boolean onBlockStartBreak(ItemStack itemstack, int i, int j, int k, EntityPlayer entityplayer)
    {
        boolean flag = false;
        ItemStack itemstack1 = null;
        World world = ModLoader.getMinecraftInstance().theWorld;
        int l = world.getBlockId(i, j, k);
        int i1 = world.getBlockMetadata(i, j, k);

        if (entityplayer.isSneaking())
        {
            return false;
        }

        ArrayList arraylist = Block.blocksList[l].getBlockDropped(world, i, j, k, i1, EnchantmentHelper.getFortuneModifier(entityplayer.inventory));
        Iterator iterator = arraylist.iterator();

        if (arraylist == null || arraylist.size() == 0)
        {
            if (l != Block.cake.blockID)
            {
                itemstack1 = new ItemStack(l, 1, i1);
                flag = true;
            }
            else
            {
                flag = true;
                itemstack1 = new ItemStack(Item.cake, 1, i1);
            }
        }

        if (iterator.hasNext() || flag)
        {
            if (iterator.hasNext())
            {
                ItemStack itemstack2 = (ItemStack)iterator.next();

                if ((itemstack2.getItem() instanceof ItemReed) || (itemstack2.getItem() instanceof ItemDoor))
                {
                    itemstack1 = itemstack2;
                }
                else
                {
                    itemstack1 = new ItemStack(l, 1, i1);
                }
            }

            world.playSoundEffect((float)i + 0.5F, (float)j + 0.5F, (float)k + 0.5F, Block.blocksList[l].stepSound.getBreakSound(), (Block.blocksList[l].stepSound.getVolume() + 1.0F) / 2.0F, Block.blocksList[l].stepSound.getPitch() * 0.8F);
            world.setBlockWithNotify(i, j, k, 0);
            float f = 0.7F;
            EntityItem entityitem = new EntityItem(world, (double)i + 0.5D, (double)j + 0.5D, (double)k + 0.5D, itemstack1);
            entityitem.delayBeforeCanPickup = 10;
            world.spawnEntityInWorld(entityitem);
            itemstack.damageItem(1, entityplayer);

            if (itemstack.getItemDamage() >= itemstack.getMaxDamage())
            {
                entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, null);
                world.playSoundAtEntity(entityplayer, "random.break", 0.5F, 1.0F);
            }
        }

        return true;
    }

    /**
     * Return an item rarity from EnumRarity
     */
    public EnumRarity getRarity(ItemStack itemstack)
    {
        return mod_KarmaCraft.ENCHANTED;
    }

    static
    {
        blocksEffectiveAgainst = (new Block[]
                {
                    Block.cobblestone, Block.stairDouble, Block.stairSingle, Block.stone, Block.sandStone, Block.cobblestoneMossy, Block.oreIron, Block.blockSteel, Block.oreCoal, Block.blockGold,
                    Block.oreGold, Block.oreDiamond, Block.blockDiamond, Block.ice, Block.netherrack, Block.oreLapis, Block.blockLapis, Block.oreRedstone, Block.oreRedstoneGlowing, Block.rail,
                    Block.railDetector, Block.railPowered
                });
    }
}
